/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utility;

/**
 *
 *
 */
import xmlMapping.*;
import CRUD.*;


import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import java.util.Set;
import java.util.Vector;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class ThreadOperationsUtility {

    // get thread markers
    public static Vector<Integer> getThreadMarkers(xmlMapping.Thread th) {

        Set markers = null;
        Vector<Integer> threadMarkers = new Vector();
        ThreadHome m = new ThreadHome();
        try {
            markers = th.getMembers();
            Iterator iterator = markers.iterator();

            while (iterator.hasNext()) {

                xmlMapping.Member member = (xmlMapping.Member) iterator.next();
                threadMarkers.add(member.getMemberId());
            }

        } catch (NullPointerException e) {
        }
        return threadMarkers;

    } //
// get fourms markerssss

    public static Vector<Integer> getForumMarkers(Forum forum) {
        Set markers = null;
        Vector<Integer> forumMarkers = new Vector();
        try {

            markers = forum.getMembers_1();
            Iterator iterator = markers.iterator();
            System.out.println("obj home");
            while (iterator.hasNext()) {

                xmlMapping.Member member = (xmlMapping.Member) iterator.next();
                forumMarkers.add(member.getMemberId());
            }

        } catch (NullPointerException e) {
        }
        return forumMarkers;

    }
    //add Thread

    public static void addThread(xmlMapping.Thread th) {
        java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        //  add thread
        System.out.println("b4 home object creation");
        ThreadHome home = new ThreadHome();
        System.out.println("after home object creation");
        home.persist(th);
        //  to update number of threads
        ForumHome forumHome = new ForumHome();
        int forumId = th.getForum().getForumId();
        xmlMapping.Forum forum = forumHome.findById(forumId);
        forum.setNoofthread(forum.getNoofthread() + 1);
        Vector<Integer> markers = new Vector();
        markers = getForumMarkers(forum);
        forumHome.attachDirty(forum);
        //send to forum markers
        for (int i = 0; i < markers.size(); i++) {
            Member member = new Member();
            member.setMemberId(markers.get(i));
            Message msg = new Message(member, th.getMember(), forum.getName(), forum.getName() + MessageUtility.THREAD_ADDED, false, sqlDate);

            System.out.println(markers.get(i));
            new MessageUtility().sendMessage(msg);
        }
    } //

//getThread
    public static xmlMapping.Thread getThread(int threadId) {
        ThreadHome m = new ThreadHome();
        xmlMapping.Thread th = m.findById(threadId);
        return th;
    }//

    // remove thread
    public static void removeThread(int threadId, Member mem) {
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        xmlMapping.Thread thread = new xmlMapping.Thread();
        ThreadHome m = new ThreadHome();
         System.out.println("#### removethread method");
        thread = m.findById(threadId);
        System.out.println("### afte findbyid thread");
        // get thread markers and tell them that removeThread
        Vector<Integer> markers = new Vector();
        markers = getThreadMarkers(thread);
        System.out.println("markers size"+markers.size());

        //////////////////////get forum matkers send them notification

        //  to update number of threads
        ForumHome forumHome = new ForumHome();
        int forumId = thread.getForum().getForumId();
        System.out.println("##forum id"+forumId);
        xmlMapping.Forum forum = forumHome.findById(forumId);
        System.out.println("after getting forum");
        forum.setNoofthread(forum.getNoofthread() - 1);
        Vector<Integer> forumMarkers = new Vector();
        forumMarkers = getForumMarkers(forum);
        System.out.println("forummarkers size"+forumMarkers.size());
        forumHome.attachDirty(forum);
        System.out.println("after attachdirty");
        // send to thread markers message

        for (int i = 0; i < markers.size(); i++) {
            Member member = new Member();
            member.setMemberId(markers.get(i));
            Message msg = new Message(member, mem, thread.getTitle(), thread.getTitle() + MessageUtility.THREAD_REMOVED, false,sqlDate );

            System.out.println(markers.get(i));
            new MessageUtility().sendMessage(msg);
        }
        //send to forum markers message
        // to tell the forum markers
        for (int i = 0; i < forumMarkers.size(); i++) {
            Member member = new Member();
            member.setMemberId(forumMarkers.get(i));
            Message msg = new Message(member, mem, forum.getName(), forum.getName() + MessageUtility.THREAD_REMOVED, false, sqlDate);

            System.out.println(forumMarkers.get(i));
            new MessageUtility().sendMessage(msg);
        }
// delete posts inisde this thread
        PostHome postHome = new PostHome();
        List posts = postHome.getPosts(thread);
        for (int i = 0; i < posts.size(); i++) {
            PostOperationsUtility postUtil = new PostOperationsUtility();
            postUtil.deletePost(((Post) posts.get(i)).getPostId());
        }
//delete thread now
        new ThreadHome().delete(thread);
    }
//edit thread

    public static void editThread(xmlMapping.Thread thread, Member mem) {
java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        ThreadHome m = new ThreadHome();
        Vector<Integer> markers = new Vector();
        markers = getThreadMarkers(thread);

        m.attachDirty(thread);

        // get the markers
        System.out.println(markers.size());
        //send to them message
        for (int i = 0; i < markers.size(); i++) {
            Member member = new Member();
            member.setMemberId(markers.get(i));
            Message msg = new Message(member, mem, thread.getTitle(), thread.getTitle() + MessageUtility.THREAD_EDITED, false, sqlDate);

            System.out.println(markers.get(i));
            new MessageUtility().sendMessage(msg);
        }

        ForumHome forumHome = new ForumHome();
        int forumId = thread.getForum().getForumId();
        xmlMapping.Forum forum = forumHome.findById(forumId);
        Vector<Integer> forumMarkers = new Vector();
        forumMarkers = getForumMarkers(forum);
        // to tell the forum markers
        for (int i = 0; i < forumMarkers.size(); i++) {
            Member member = new Member();
            member.setMemberId(forumMarkers.get(i));
            Message msg = new Message(member, mem, forum.getName(), thread.getTitle() + MessageUtility.THREAD_EDITED, false,sqlDate);

            System.out.println(forumMarkers.get(i));
            new MessageUtility().sendMessage(msg);
        }
//
    }
// main for test

    public static void main(String[] args) {

//        xmlMapping.Thread th = new xmlMapping.Thread();
//        th.setBlocked(false);
//        th.setBody("7beby");
//        Forum forum = new Forum();
//        forum.setForumId(1);
//        th.setForum(forum);
//        th.setLocked(false);
//        th.setThreadId(20);
        Member member = new Member();
        member.setMemberId(1);
        removeThread(23,member);
//        th.setMember(member);
//        th.setTitle("emooo");
//        th.setSticky(false);
//        th.setNoOfVisitor(0);
//        th.setLastModified(new Date());
//        Set markers = new HashSet(0);
//        Member member1 = new Member();
//        member1.setMemberId(new Integer(2));
//        Member member2 = new Member();
//        member2.setMemberId(new Integer(1));
//        markers.add(member2);
//        markers.add(member1);
//        th.setMembers(markers);

//addThread(th);
//    xmlMapping.Thread r=  getThread(15);
//     System.out.println(r.getBody());

//        Member sender = new Member();
//        sender.setMemberId(4);
//       editThread(th,sender);
      }


}
